<template>
	<div v-cloak class="aui-wrapper aui-page__iframe">
		<div class="aui-content__wrapper">
			<div class="aui-content">
				<el-card shadow="never" class="aui-card--fill">
					<div class="mod-lswms__check">
						<el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
							<el-form-item>
								<el-input v-model="dataForm.id" size="small" placeholder="id" clearable></el-input>
							</el-form-item>
							<el-form-item>
								<el-button size="small" @click="getDataList()">查询</el-button>
							</el-form-item>
							<el-form-item>
								<el-button type="primary" @click="addOrUpdateHandle()" size="small"
										   v-if="hasPermission('lswms:check:save')" >新增</el-button>
							</el-form-item>
							<el-form-item>
								<el-button type="primary" @click="createCheckFn()"  size="small"
										   v-if="hasPermission('lswms:check:createcheck')">生成盘点明细</el-button>
							</el-form-item>
							<el-form-item>
								<el-button type="primary" @click="createCheckTaskFn()" size="small"
										   v-if="hasPermission('lswms:check:createcheckTask')">生成盘点任务</el-button>
							</el-form-item>
							
							<el-form-item>
								<el-button type="primary" @click="createCheckPostFn()"  size="small"
										   v-if="hasPermission('lswms:check:CheckPost')">盘点过账</el-button>
							</el-form-item>
							
							<el-form-item>
								<el-button type="primary" @click="cancelCheckPostFn()"  size="small"
										   v-if="hasPermission('lswms:check:cancelCheckPost')">盘点取消</el-button>
							</el-form-item>
							
							<el-form-item>
								<el-button type="danger" @click="deleteHandle()" size="small"
										   v-if="hasPermission('lswms:check:delete')">删除</el-button>
							</el-form-item>
							<el-form-item>
								<el-button type="info" @click="exportHandle()" size="small"
										   v-if="hasPermission('lswms:check:export')">导出</el-button>
							</el-form-item>
						</el-form>
						<el-table v-loading="dataListLoading" :data="dataList" border
							@selection-change="dataListSelectionChangeHandle" @sort-change="dataListSortChangeHandle"
							style="width: 100%;">
							<el-table-column type="selection" header-align="center" align="center" width="50">
							</el-table-column>
							<!--   <el-table-column prop="id" label="序号"  header-align="center"
                                           align="center"></el-table-column> -->
							<el-table-column prop="checkkey" label="盘点号" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="status" label="状态" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="fromsku" label="自物料" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="tosku" label="至物料" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="fromzone" label="自区" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="tozone" label="至区" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="fromloc" label="自库位" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="toloc" label="至库位" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="begintime" label="盘点开始时间" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="endtime" label="盘点结束时间" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="remark" label="备注" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="backstatus" label="回传状态" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="backdate" label="回传时间" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="backresult" label="回传结果" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="backcode" label="回传码" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="warehouse" label="仓库" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="adduser" label="创建人" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="addtime" label="创建时间" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="updatuser" label="修改人" header-align="center" align="center">
							</el-table-column>
							<el-table-column prop="updatetime" label="修改时间" header-align="center" align="center">
							</el-table-column>
							<el-table-column label='操作' fixed="right" header-align="center" align="center" width="150">
								<template #default="scope">
									<el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)"  v-if="hasPermission('lswms:check:update')">修改
									</el-button>
									<el-button type="text" size="small" @click="listHandle(scope.row.checkkey)"  >明细
									</el-button>
									<el-button type="text" size="small" @click="deleteHandle(scope.row.id)"  v-if="hasPermission('lswms:check:delete')">删除
									</el-button>
								</template>
							</el-table-column>
						</el-table>
						<el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit"
							:total="total" layout="total, sizes, prev, pager, next, jumper"
							@size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle">
						</el-pagination>
						<!-- 弹窗, 新增 / 修改 -->
						<checkAddOrUpdate v-if="addOrUpdateVisible" ref="addOrUpdate" @refresh-data-list="getDataList">
						</checkAddOrUpdate>
						
						<!-- 明细 -->
						<checkdetail v-if="checkdetailVisible" ref="checkdetailvue"></checkdetail>
					</div>
				</el-card>
			</div>
		</div>
	</div>
</template>

<script>
	import DataList from "@/utils/DataList";
	import {
		ref,
		reactive,
		onMounted,
		nextTick
	} from 'vue';
	import checkAddOrUpdate from "@/components/lswms/check/CheckAddOrUpdate";
		import checkdetail from "@/views/lswms/check/Checkdetail";
	import {createCheck,createCheckTask,createCheckPost} from "@/network/lswms/check/Check"
	import {
		ElMessage,
		ElMessageBox
	} from 'element-plus'

	import {hasPermission} from "@/utils/hasPermission"
	export default {
		name: "check",

		components: {
			checkAddOrUpdate,
			checkdetail
		},

		setup() {
			const {
				dataForm,
				mixinViewModuleOptions,
				getDataList,
				dataList,
				total,
				page,
				limit,
				dataListLoading,
				dataListSelectionChangeHandle,
				dataListSortChangeHandle,
				pageSizeChangeHandle,
				pageCurrentChangeHandle,
				addOrUpdateHandle,
				deleteHandle,
				exportHandle,
				addOrUpdateVisible,
				dataListSelections,
				addOrUpdate
			} = DataList();

			onMounted(() => {
				mixinViewModuleOptions.value.getDataListIsPage = true;
				mixinViewModuleOptions.value.getDataListURL = '/lswms/check/page';
				mixinViewModuleOptions.value.deleteURL = '/lswms/check';
				mixinViewModuleOptions.value.exportURL = '/lswms/check/export';
				mixinViewModuleOptions.value.deleteIsBatchKey = 'id';
				mixinViewModuleOptions.value.deleteIsBatch = true
				if (mixinViewModuleOptions.value.activatedIsNeed) {
					getDataList();
				}

			})


			const createCheckFn = () => {
				if (dataListSelections.value.length <= 0) {
					return ElMessage({
						message: '请选择盘点单',
						type: 'warning',
						duration: 1000
					});
				}
				if (dataListSelections.value.length > 1) {
					return ElMessage({
						message: '只能选择一条记录',
						type: 'warning',
						duration: 1000
					});
				}
				ElMessageBox.confirm('确定要生成盘点明细吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					let para = dataListSelections.value.map(function(item) {
						return {
							"checkkey": item["checkkey"]
						}
					})
					createCheck(para).then(function(res) {
						if (res.code != 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								getDataList();
							}
						});
					}).catch(function() {});

				}).catch(function() {})
			}

			const createCheckTaskFn = () => {
				if (dataListSelections.value.length <= 0) {
					return ElMessage({
						message: '请选择盘点单',
						type: 'warning',
						duration: 1000
					});
				}
				if (dataListSelections.value.length > 1) {
					return ElMessage({
						message: '只能选择一条记录',
						type: 'warning',
						duration: 1000
					});
				}
				ElMessageBox.confirm('确定要生成盘点明细吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					let para = dataListSelections.value.map(function(item) {
						return {
							"checkkey": item["checkkey"],
                            "fromloc" : item["fromloc"],
                            "toloc" : item["toloc"]
						}
					})
					createCheckTask(para).then(function(res) {
						if (res.code != 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								getDataList();
							}
						});
					}).catch(function() {});

				}).catch(function() {})
			}
			
			
			//盘点过账
			const createCheckPostFn = () => {
				if (dataListSelections.value.length <= 0) {
					return ElMessage({
						message: '请选择盘点单',
						type: 'warning',
						duration: 1000
					});
				}
				if (dataListSelections.value.length > 1) {
					return ElMessage({
						message: '只能选择一条记录',
						type: 'warning',
						duration: 1000
					});
				}
				ElMessageBox.confirm('确定要过账盘点单吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					let para = dataListSelections.value.map(function(item) {
						return {
							"checkkey": item["checkkey"]
						}
					})
					createCheckPost(para).then(function(res) {
						if (res.code != 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								getDataList();
							}
						});
					}).catch(function() {});
			
				}).catch(function() {})
			}
			
			const cancelCheckPostFn = () => {
				if (dataListSelections.value.length <= 0) {
					return ElMessage({
						message: '请选择盘点单',
						type: 'warning',
						duration: 1000
					});
				}
				if (dataListSelections.value.length > 1) {
					return ElMessage({
						message: '只能选择一条记录',
						type: 'warning',
						duration: 1000
					});
				}
				ElMessageBox.confirm('确定要过账盘点单吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(function() {
					let para = dataListSelections.value.map(function(item) {
						return {
							"checkkey": item["checkkey"]
						}
					})
					cancelCheckPost(para).then(function(res) {
						if (res.code != 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								getDataList();
							}
						});
					}).catch(function() {});
			
				}).catch(function() {})
			}
			

// <checkdetail v-if="checkdetailVisible" ref="checkdetailvue"></checkdetail>

			const checkdetailVisible = ref(false);
			const checkdetailvue = ref(null);

			//打开list页
			const listHandle = (id, checkkey) => {
				checkdetailVisible.value = true;
				nextTick(function() {
					checkdetailvue.value.dataForm.checkkey = checkkey;
					checkdetailvue.value.init();
				})
			}


			return {
				dataForm,
				mixinViewModuleOptions,
				getDataList,
				dataList,
				total,
				page,
				limit,
				dataListLoading,
				dataListSelectionChangeHandle,
				dataListSortChangeHandle,
				pageSizeChangeHandle,
				pageCurrentChangeHandle,
				addOrUpdateHandle,
				deleteHandle,
				exportHandle,
				addOrUpdateVisible,
				dataListSelections,
				addOrUpdate,
				createCheckFn,
				createCheckTaskFn,
				checkdetailVisible,
				checkdetailvue,
				listHandle,
				createCheckPostFn,
				cancelCheckPostFn
			}
		},
		methods:{
			hasPermission
		}
	}
</script>

<style scoped>

</style>
